<?php
	require("frmCookie.php");

	$iOpcion = $_GET[iOpcion];
	$idsModelos = $_REQUEST['idsModelos'];
?>
<html>
	<head>
    	<?php
		require("frmHeader.php");
		?>
	</head>
	<body>
    	<table cellpadding="0" cellspacing="0" align="center" class="tablaPrincipal">
        	<tr>
            	<td align="center">
                	<?php
					require("frmSession.php"); 
					
					//Redirecciona a SINPERMISOS.PHP en caso de no tener permisos
					
					if (!(in_array(30000, $_SESSION[permisos])))
					{
					?>
					  <script language="JavaScript" type="text/javascript">
					  <!--
					  window.location.href = "sinPermisos.php"
					  //-->
					  </script>
					<?php
					}
					$titulo = "Fijacion de Stock";
					
					?>
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<?php
					require("frmTitulo.php");
					?>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="menuPrincipal">
                    	<tr>
                        	<td>
								<?php
                                require("frmMenu.php");
                                ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="tituloPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $titulo; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="estadoPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $mensaje; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="contenedorPrincipal">
                    	<tr>
                        	<td align="">
                        		<?php

									$mySQL = "DELETE FROM fijacionstock WHERE idSucursal=" . $_POST['idSucursal'] . " AND idParte in (select idParte from partesxmodelo where idModelo in ($idsModelos))";
									executeSQL($mySQL);
								
									$mySQL = "SELECT DISTINCT(P.codigoTango), ";
									$mySQL = $mySQL . "MC.nombre AS MARCA, ";
									$mySQL = $mySQL . "P.codigoTango, ";
									$mySQL = $mySQL . "P.idParte, ";
									$mySQL = $mySQL . "P.descripcionInterna, ";
									$mySQL = $mySQL . "P.descripcionFabricante, ";
									$mySQL = $mySQL . "TP.nombre as TIPOPARTE ";
									$mySQL = $mySQL . "FROM partes P ";
									$mySQL = $mySQL . "LEFT JOIN tipospartes TP ON TP.idTipoParte = P.idTipoParte ";
									$mySQL = $mySQL . "LEFT JOIN partesxmodelo PXM ON PXM.idParte = P.idParte ";
									$mySQL = $mySQL . "LEFT JOIN modelos M ON PXM.idModelo = M.idModelo ";
									$mySQL = $mySQL . "LEFT JOIN marcas MC ON MC.idMarca = M.idMarca ";
									$mySQL = $mySQL . "LEFT JOIN fijacionstock FS ON FS.idParte = P.idParte ";
									$mySQL = $mySQL . "WHERE MC.idMarca = " . $_POST['idMarca'] . " AND P.descripcionInterna <> 'N.U.P' AND P.descripcionFabricante <> 'N.U.P' AND PXM.idModelo in ($idsModelos) GROUP BY P.codigoTango";
									
									$result = readSQL($mySQL);
											
									$sucursal = $_POST['idSucursal'];
												
									$i = 0;	
									foreach($result as $value)
									{
										$field = "txt" . $value['codigoTango'];
										if($_POST[$field] != "")
										{
											$cantidad = $_POST[$field];
											$mySQL = "INSERT INTO fijacionstock(idParte, idSucursal, cantidad, idMarca) VALUES(";
											$mySQL = $mySQL . $value['idParte'] . ", ";
											$mySQL = $mySQL . $sucursal . ", ";
											$mySQL = $mySQL . $cantidad . ", ";
											$mySQL = $mySQL . $_POST['idMarca'] . ")";
										}
										else
										{
											$mySQL = "INSERT INTO fijacionstock(idParte, idSucursal, idMarca) VALUES(";
											$mySQL = $mySQL . $value['idParte'] . ", ";
											$mySQL = $mySQL . $sucursal . ", ";
											$mySQL = $mySQL . $_POST['idMarca'] . ")";	
										}
										
										executeSQL($mySQL);
									}
									
									echo "Fijacion de Stock Realizada con exito";
								?>
		                     </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
	</body>
</html>